import Api from '@/api/api'
class Tool {
  static getList(vue, paramFilters) {
    const params = {
      page: vue.page,
      order: vue.order,
      sortby: vue.sortby
    }
    Object.assign(params, paramFilters)
    vue.listLoading = true
    Api.service
      .list(vue.pageController, params)
      .then(data => {
        vue.total = data.total
        vue.items = data.items
        vue.listLoading = false
      })
      .catch(() => {
        vue.listLoading = false
      })
  }
  static sortChange(vue, column) {
    if (column.order === 'ascending') {
      vue.order = 'asc'
    } else if (column.order === 'descending') {
      vue.order = 'desc'
    }
    vue.sortby = column.prop
    vue.getList()
  }
  static getMenus(vue) {
    const params = {}
    vue.listLoading = true
    Api.service
      .menus(vue.pageController, params)
      .then(data => {
        Object.keys(vue.menus).forEach(function(key) {
          if (Object.prototype.hasOwnProperty.call(data, key)) {
            vue.menus[key] = data[key]
          }
        })
      })
      .catch(() => {
        vue.listLoading = false
      })
  }
  static handleDel(vue, pk) {
    vue
      .$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      })
      .then(() => {
        vue.listLoading = true
        const params = pk
        Api.service
          .remove(vue.pageController, params)
          .then(res => {
            vue.listLoading = false
            vue.$message({
              message: '删除成功',
              type: 'success'
            })
            vue.getList()
          })
          .catch(() => {
            vue.listLoading = false
          })
      })
  }
  static dbSubmit(vue) {
    vue.$refs.dbForm.validate(valid => {
      if (valid) {
        vue.$confirm('确认提交吗？', '提示', {}).then(() => {
          vue.dbLoading = true
          const params = Object.assign({}, vue.dbForm)
          if (vue.dbMethod === 'create') {
            Api.service
              .add(vue.pageController, params)
              .then(res => {
                vue.dbSuccess()
              })
              .catch(() => {
                vue.dbLoading = false
              })
          } else {
            Api.service
              .edit(vue.pageController, params)
              .then(res => {
                vue.dbSuccess()
              })
              .catch(() => {
                vue.dbLoading = false
              })
          }
        })
      }
    })
  }
  static dbSuccess(vue) {
    vue.dbLoading = false
    vue.$message({
      message: '提交成功',
      type: 'success'
    })
    vue.$refs['dbForm'].resetFields()
    vue.dbFormVisible = false
    vue.getList()
  }
  static batchRemove(vue, ids) {
    vue
      .$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      })
      .then(() => {
        vue.listLoading = true
        const params = { ids: ids }
        Api.service
          .batchRemove(vue.pageController, params)
          .then(res => {
            vue.listLoading = false
            vue.$message({
              message: '删除成功',
              type: 'success'
            })
            vue.getList()
          })
          .catch(() => {
            vue.listLoading = false
          })
      })
      .catch(() => {})
  }
  static FINAL = 6378137.0

  /**
   * 求某个经纬度的值的角度值
   * @param {Object} d
   */

  static calcDegree(d) {
    return (d * Math.PI) / 180.0
  }

  /**
   * 根据两点经纬度值，获取两地的实际相差的距离
   * @param {Object} f    第一点的坐标位置[latitude,longitude]
   * @param {Object} t    第二点的坐标位置[latitude,longitude]
   */

  static calcDistance(f, t) {
    var flat = this.calcDegree(f[0])
    var flng = this.calcDegree(f[1])
    var tlat = this.calcDegree(t[0])
    var tlng = this.calcDegree(t[1])

    var result = Math.sin(flat) * Math.sin(tlat)
    result += Math.cos(flat) * Math.cos(tlat) * Math.cos(flng - tlng)
    return Math.acos(result) * this.FINAL
  }

  static isJsonString(str) {
    try {
      JSON.parse(str)
    } catch (e) {
      return false
    }
    return true
  }

  static statusFilter(status) {
    const statusMap = {
      1: 'success',
      0: 'danger',
      '-1': 'info'
    }
    return statusMap[status]
  }

  static adminFilter(row) {
    let status = row.isAdmin
    if (row.userId === 1) {
      status = 100
    }
    const statusMap = {
      1: 'warning',
      0: '',
      100: 'danger'
    }
    return statusMap[status]
  }

  static checkImage(file) {
    // 上传大图片注意修改NGINX的client_max_body_size
    const errors = []
    const isIMAGE = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif'
    const isLt2M = file.size / 1024 / 1024 < 2
    if (!isIMAGE) {
      errors.push('上传缩略图图片只能是图片格式!')
    }
    if (!isLt2M) {
      errors.push('上传缩略图图片大小不能超过 2MB!')
    }
    return errors
  }
}
export default Tool
